// Copyright (C) 2021 Institute of Data Security, HIT
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "gtest/gtest.h"
#include "hsmc/connector.h"
#include "hsmc/session_pool.h"
#include "hsmc/exception.h"
#include "hsmc/svs/ndsec/session.h"
#include "hsmc/svs/sinocipher/session.h"

TEST(SVSSessionTest, GetSession) {
  auto session = ::hsmc::SessionPool::instance().get(hsmc::ConnectorType::CT_SVS);

  EXPECT_TRUE(session.isGood());
}

TEST(SVSSessionTest, VerifySignedDataAndCert) {
  auto session = ::hsmc::SessionPool::instance().get(hsmc::ConnectorType::CT_SVS);

  const unsigned char signData[] = {
      0x30, 0x45, 0x02, 0x21, 0x00, 0x81, 0x14, 0xcc, 0x16, 0xbd, 0xfd, 0x50, 0xef, 0xd3, 0x99, 0x39, 0x91, 0x72,
      0xc4, 0xeb, 0x15, 0xf4, 0x70, 0x0a, 0x1b, 0x72, 0x60, 0xe0, 0x72, 0x64, 0xaa, 0x02, 0x2d, 0x8f, 0x27, 0x8a,
      0x04, 0x02, 0x20, 0x09, 0x3a, 0xa7, 0x68, 0xf0, 0x0e, 0xae, 0x69, 0x36, 0xfa, 0xd2, 0xe7, 0xdf, 0x6c, 0xe2,
      0xc3, 0xa0, 0xb4, 0x97, 0x17, 0xd7, 0x20, 0xdb, 0xa8, 0xc8, 0xb3, 0xe4, 0x3b, 0xe5, 0xf4, 0x0b, 0x0a};
  const unsigned char certData[] = {
      0x30, 0x82, 0x02, 0x1e, 0x30, 0x82, 0x01, 0xc4, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x01, 0x01, 0x30, 0x0a, 0x06,
      0x08, 0x2a, 0x81, 0x1c, 0xcf, 0x55, 0x01, 0x83, 0x75, 0x30, 0x56, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
      0x06, 0x13, 0x02, 0x43, 0x4e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x07, 0x62, 0x65, 0x69,
      0x6a, 0x69, 0x6e, 0x67, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x51, 0x49, 0x41, 0x4e,
      0x58, 0x49, 0x4e, 0x31, 0x0e, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x05, 0x51, 0x55, 0x49, 0x43, 0x4b,
      0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0a, 0x51, 0x55, 0x49, 0x43, 0x4b, 0x53, 0x55, 0x42,
      0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x30, 0x37, 0x32, 0x35, 0x30, 0x31, 0x35, 0x34, 0x34, 0x37, 0x5a,
      0x17, 0x0d, 0x32, 0x35, 0x30, 0x37, 0x32, 0x34, 0x30, 0x31, 0x35, 0x34, 0x34, 0x37, 0x5a, 0x30, 0x51, 0x31, 0x0b,
      0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x43, 0x4e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
      0x08, 0x0c, 0x07, 0x62, 0x65, 0x69, 0x6a, 0x69, 0x6e, 0x67, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a,
      0x0c, 0x07, 0x51, 0x49, 0x41, 0x4e, 0x58, 0x49, 0x4e, 0x31, 0x0e, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c,
      0x05, 0x51, 0x55, 0x49, 0x43, 0x4b, 0x31, 0x0e, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x05, 0x73, 0x75,
      0x62, 0x30, 0x31, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
      0x81, 0x1c, 0xcf, 0x55, 0x01, 0x82, 0x2d, 0x03, 0x42, 0x00, 0x04, 0x22, 0xc5, 0x48, 0x51, 0x53, 0xd6, 0x8f, 0x7e,
      0x06, 0x4f, 0x36, 0x08, 0x77, 0x00, 0x11, 0x27, 0xd7, 0xa5, 0x20, 0xc0, 0x92, 0xaf, 0xcc, 0x22, 0xeb, 0x35, 0x95,
      0x50, 0x92, 0x45, 0xec, 0xb7, 0xd5, 0x94, 0x55, 0x5b, 0xeb, 0xb5, 0x13, 0xad, 0x4a, 0x21, 0x8f, 0xbd, 0xd1, 0x08,
      0x55, 0x54, 0xd2, 0xe7, 0xfb, 0x49, 0xf1, 0xe9, 0xd3, 0xfe, 0x5b, 0x16, 0x3f, 0xd9, 0xb7, 0x1a, 0xac, 0x74, 0xa3,
      0x81, 0x87, 0x30, 0x81, 0x84, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x0b, 0x06,
      0x03, 0x55, 0x1d, 0x0f, 0x04, 0x04, 0x03, 0x02, 0x07, 0x80, 0x30, 0x2a, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x86,
      0xf8, 0x42, 0x01, 0x0d, 0x04, 0x1d, 0x16, 0x1b, 0x47, 0x6d, 0x53, 0x53, 0x4c, 0x20, 0x47, 0x65, 0x6e, 0x65, 0x72,
      0x61, 0x74, 0x65, 0x64, 0x20, 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x30, 0x1d, 0x06,
      0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x06, 0xbf, 0xfb, 0x16, 0xa1, 0x12, 0xb8, 0x8d, 0xc3, 0xab, 0x17,
      0xa8, 0x18, 0x7b, 0xaf, 0xc3, 0xfe, 0x63, 0x21, 0x49, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30,
      0x16, 0x80, 0x14, 0x4a, 0x40, 0x42, 0xb5, 0x3d, 0x8b, 0xea, 0x53, 0xf8, 0x45, 0x06, 0xc2, 0x4f, 0x06, 0x02, 0xbc,
      0xa0, 0x32, 0x7f, 0x0d, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x81, 0x1c, 0xcf, 0x55, 0x01, 0x83, 0x75, 0x03, 0x48, 0x00,
      0x30, 0x45, 0x02, 0x20, 0x33, 0x85, 0xf2, 0xc9, 0x3a, 0x87, 0x65, 0x45, 0x01, 0x49, 0x64, 0x23, 0x67, 0x66, 0x4d,
      0xd4, 0xf0, 0xed, 0xb0, 0x92, 0x67, 0x21, 0xff, 0xe7, 0x72, 0x64, 0x1c, 0x05, 0x39, 0x22, 0xf4, 0x73, 0x02, 0x21,
      0x00, 0xcb, 0x4d, 0xf2, 0x4d, 0x07, 0x09, 0x00, 0x79, 0xf9, 0xed, 0x64, 0xad, 0xb8, 0x23, 0x2b, 0xc8, 0xa1, 0xe5,
      0x93, 0xb4, 0x61, 0x77, 0xb5, 0xdb, 0x91, 0x3d, 0xfa, 0xe9, 0x6d, 0xb7, 0xe6, 0xa3};

  std::cout << "begin verify cert time" << std::endl;
  int result = session.SVS_VerifySignedData(1, certData, sizeof(certData), (const unsigned char *)"123456", 6, signData,
                                            sizeof(signData), 0);
  EXPECT_EQ(0, result);

  std::cout << "begin verify cert sign" << std::endl;
  result = session.SVS_VerifySignedData(1, certData, sizeof(certData), (const unsigned char *)"123456", 6, signData,
                                        sizeof(signData), 1);
  EXPECT_EQ(0, result);

  std::cout << "begin verify cert crl" << std::endl;
  result = session.SVS_VerifySignedData(1, certData, sizeof(certData), (const unsigned char *)"123456", 6, signData,
                                        sizeof(signData), 2);
  EXPECT_EQ(0, result);
}

TEST(SVSSessionTest, SinocipherGenRandom) {
  auto session = ::hsmc::SessionPool::instance().getByConnector("sinocipher-svs_node1");

  auto svs_session = ::hsmc::svs::sinocipher::Session(session);

  uint8_t randomData[6] = {0};
  int result = svs_session.SVS_Random(6, randomData);
  EXPECT_EQ(0, result);
}

TEST(SVSSessionTest, NdsecGenRandom) {
  auto session = ::hsmc::SessionPool::instance().getByConnector("ndsec-svs_node1");

  auto svs_session = ::hsmc::svs::ndsec::Session(session);

  uint8_t randomData[6] = {0};
  int result = svs_session.SVS_GenerateRandom(6, randomData);
  EXPECT_EQ(0, result);
}

TEST(SVSSessionTest, PoolingTest) {
  std::string sid;
  {
    auto session = ::hsmc::SessionPool::instance().getByConnector("sinocipher-svs_node1");
    sid = session.getId();
  }
  try {
    auto session = ::hsmc::SessionPool::instance().get(sid);
    EXPECT_TRUE(session.isGood());
  } catch (hsmc::NotFoundException &ex) {
    // 不应该抛出NotFound异常
    EXPECT_TRUE(false);
  }
}

TEST(SVSSessionTest, GetDevSnByNdsecSvsNode1) {
  auto session = ::hsmc::SessionPool::instance().getByConnector("ndsec-svs_node1");
  DeviceInfo_st di = {0};
  int rc = session.SDF_GetDeviceInfo(&di);
  EXPECT_EQ(rc, 0);
  std::string SN((const char *)di.DeviceSerial, 16);
  std::cout << "The SN of ndsec-svs_node1 is: " << SN << std::endl;
}